﻿using System;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.Data;
using System.Text;
using Common.Module.Permissions.Model;
namespace Common.Module.Permissions
{
    public class Handle
    {
        /// <summary>
        /// 得到用户的所有权限
        /// </summary>
        /// <param name="uid">用户编号</param>
        /// <returns>权限列表</returns>
        public List<Module.Permissions.Model.T_Action> GetMasterGroupAction(int UserID)
        {
            string sql = "select * from t_action where id in (select actionid from t_actiongroup where groupid in (select groupid from t_mastergroup where masterid=" + UserID + "))";
            OracleDataReader sdr = ADO.Tools.OracleHelper.ExecuteReader(sql);
            List<Module.Permissions.Model.T_Action> actions = new List<Module.Permissions.Model.T_Action>();
            while (sdr.Read())
            {
                Module.Permissions.Model.T_Action a = new Module.Permissions.Model.T_Action();
                a.ActionName = sdr["ActionName"].ToString();
                a.Description = sdr["Description"].ToString();
                a.Id = Convert.ToInt32(sdr["Id"]);
                actions.Add(a);
            }
            sdr.Close();
            return actions;
        }

        /// <summary>
        /// 增加一个权限
        /// </summary>
        /// <param name="action"></param>
        public void InsertAction(Module.Permissions.Model.T_Action action)
        {
            ADO.DBManager.Insert(action);
        }
        /// <summary>
        /// 增加一个角色
        /// </summary>
        /// <param name="groupManager"></param>
        public void InsertGroupManager(T_GroupManager groupManager)
        {
            ADO.DBManager.Insert(groupManager);
        }
        /// <summary>
        /// 为角色添加一个权限
        /// </summary>
        /// <param name="actionGroup"></param>
        public void InsertActionGroup(T_ActionGroup actionGroup)
        {
            ADO.DBManager.Insert(actionGroup);
        }
        /// <summary>
        /// 为用户添加一个角色
        /// </summary>
        /// <param name="masterGroup"></param>
        public void InsertMasterGroup(T_MasterGroup masterGroup)
        {
            ADO.DBManager.Insert(masterGroup);
        }

        /// <summary>
        /// 查看用户是否有某项权限
        /// </summary>
        /// <param name="masterGroup"></param>
        /// <returns></returns>
        public bool isExistAction(int UserID, int actionId)
        {
            bool isExist = false;
            List<Module.Permissions.Model.T_Action> actions = GetMasterGroupAction(UserID);
            foreach (Module.Permissions.Model.T_Action a in actions)
            {
                if (a.Id == actionId)
                {
                    isExist = true;
                    break;
                }
            }
            return isExist;
        }


        /// <summary>
        /// 得到所有权限
        /// </summary>
        /// <returns></returns>
        public List<object> GetAllAction()
        {
            return ADO.DBManager.GetList(new Module.Permissions.Model.T_Action());
        }

        /// <summary>
        /// 得到所有角色
        /// </summary>
        /// <returns></returns>
        public List<object> GetAllGroupManager()
        {
            return ADO.DBManager.GetList(new Module.Permissions.Model.T_GroupManager());
        }



    }
}
